package com.atguigu.juc;

import java.util.Random;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.function.Supplier;

public class TestCompleteblaFuture {


    public static void main(String[] args) throws ExecutionException, InterruptedException {

        ExecutorService pool = Executors.newFixedThreadPool(10);

        for (int i = 0; i < 10; i++) {
            CompletableFuture.supplyAsync(new MyThread(), pool).whenComplete((result, exception) -> {
                System.out.println(Thread.currentThread().getName() + " : " + result);
            });

        }
        pool.shutdown();
    }
}


class MyThread implements Supplier<Integer> {

    @Override
    public Integer get() {
        int sum = 0;
        System.out.println(Thread.currentThread().getName() + " : 运算中");
        try {
            Thread.sleep(5 * 1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return new Random().nextInt(100);
    }

}
